package com.zmn.oms.admin.controller.work.modify;

import com.alibaba.fastjson.JSON;
import com.zmn.oms.admin.controller.work.WorkBaseController;
import com.zmn.oms.admin.thymeleaf.OrderAuthPermit;
import com.zmn.oms.business.interfaces.messageV1.text.MessageSendTextService;
import com.zmn.oms.common.constant.MessageRuleIdConsts;
import com.zmn.oms.common.constant.OrderPremissionConsts;
import com.zmn.oms.common.dto.MessageTemplate;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.model.dto.messageV1.TextMessageDTO;
import com.zmn.oms.model.dto.work.modify.OrderSendSmsDTO;
import com.zmn.oms.model.entity.detail.OrderDetail;
import com.zmn.oms.model.entity.work.OrderWork;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 类描述：发送短信
 *
 * @author liuying
 * @date 2020-10-28 11:09:19
 */
@Controller
@RequestMapping("/work/modify")
public class WorkSendSmsController extends WorkBaseController {

    @Autowired
    private MessageSendTextService messageSendTextService;

    /**
     * 发送短信
     */
    @RequestMapping(value = "/send/sms")
    @ResponseBody
    public ResultDTO updateSignIn(@RequestBody @Valid OrderSendSmsDTO sendSmsDTO) {
        boolean hasPermit = OrderAuthPermit.getInstance().checkDetailPermit(sendSmsDTO.getModuleId(), OrderPremissionConsts.ODP_SEND_SMS);
        if (!hasPermit) {
            return ResultDTO.fail("没有操作权限");
        }

        if (StringUtils.isBlank(sendSmsDTO.getMobile())) {
            return ResultDTO.fail("发送手机号码不能为空");
        }

        OrderWork orderWork = orderWorkService.findOrderWorkByKey(sendSmsDTO.getOrderId(), sendSmsDTO.getWorkId());
        if (Objects.isNull(orderWork)) {
            return ResultDTO.fail("not find order");
        }

        Integer ruleId = sendSmsDTO.getGroupId();
        // 查找 ruleId 配置
        MessageTemplate messageTemplate = MessageRuleIdConsts.YEYX_MESSAGE_TO_USER_LIST.stream().filter(item -> Objects.equals(item.getGroupId(), ruleId)).findFirst()
                .orElse(MessageRuleIdConsts.YEYX_MESSAGE_TO_MASTER_LIST.stream().filter(item -> Objects.equals(item.getGroupId(), ruleId)).findFirst().orElse(null));
        if (Objects.isNull(messageTemplate)) {
            return ResultDTO.fail(String.format("短信发送失败：未找到模板组[%s]配置", ruleId));
        }

        OrderDetail orderDetail = orderDetailService.findOrderDetailByKey(sendSmsDTO.getOrderId());
        Map<String, String> smsMap = new HashMap<>();
        smsMap.put("user_telephone", orderDetail.getTelephone());

        // 发送消息
        TextMessageDTO textMessage = TextMessageDTO.builder()
                .mobile(sendSmsDTO.getMobile())
                .ruleId(ruleId)
                .channelId(String.valueOf(orderWork.getChannelId()))
                .cityId(String.valueOf(orderWork.getCityId()))
                .params(JSON.toJSONString(smsMap))
                .build();
        boolean isSuccess = messageSendTextService.sendTextMessage(textMessage);

        return ResultDTO.success(isSuccess);
    }
}
